package data.processor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import util.FileDealer;

/**
 * @author Fang Lu, fanglupku@gmail.com
 */

public class ExceptionPageDealer extends BasePageDealer{
	public static void main(String args[])
	{
		try {
			String text = FileDealer.getFileContent("C:\\Users\\ftc\\Desktop\\代码块提取\\test_page\\3_java-examples.txt", "GBK");
			text = ExceptionPageDealer.eraseAllComment(text);
			text = ExceptionPageDealer.eraseOneTagAndContent(text, "head");
			text = ExceptionPageDealer.eraseOneTagAndContent(text, "script");
			text = ExceptionPageDealer.eraseOneTagAndContent(text, "style");
			text = text.replaceAll("<br((\\s*?)|(\\s+?.*?))>", "\n");
			ArrayList<String> tagList = new ArrayList<String>();
			tagList.add("code");
			tagList.add("pre");
			tagList.add("div");
			tagList.add("p");
			tagList.add("td");
			tagList.add("ol");
			tagList.add("ul");
			ArrayList<SegmentItem> tList = getTextFragmentInPage(text, tagList);
			tList = HTMLEntityTransform.segmentItemBatchTransform(tList, null);
			/**
			 * filter empty text
			 */
			tList = filterEmptySegmentItem(tList, null);
			
			/**
			 * merge some codes together
			 */
			
//			tList = CodeMerger.continualCommentMerge(tList);
//			tList = CodeMerger.continualCodeMerge(tList);
			
			/**
			 * split text into code and non-code to help compile
			 */
			/*
			 * .............................to be continue
			 */
			
			for (int i=0; i<tList.size(); ++i)
			{
//				if (!CodeJudge.isCode(tList.get(i)))
//				{
//					continue;
//				}
				System.out.println(i + "+-----------------" + tList.get(i).getBeginTag());
				System.out.println("==" + tList.get(i).getIndex());
				System.out.println(tList.get(i).getContent());
				System.out.println(i + "*-----------------" + tList.get(i).getEndTag());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static ArrayList<String> getTextFragmentInOneTag(String originString, String tag)
	{
		ArrayList<String> resultList = new ArrayList<String>();
		ArrayList<String> rawList = extractOneTagContent(originString, tag);
		for (int i=0; i<rawList.size(); ++i)
		{
			if (rawList.get(i) != null)
			{
				String thisFragment = eraseAllTags(rawList.get(i));
				resultList.add(new String(thisFragment));
			}
		}
		return resultList;
	}
	
	public static ArrayList<SegmentItem> getTextFragmentInPage(String originString, ArrayList<String> segTagList)
	{
		ArrayList<SegmentItem> resultList = new ArrayList<SegmentItem>();
		ArrayList<String> borderList = new ArrayList<String>();
		ArrayList<Integer> segPosList = new ArrayList<Integer>();
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<segTagList.size(); ++i)
		{
			if (i != 0)
			{
				sb.append("|");
			}
			sb.append(segTagList.get(i));
		}
		String str_pattern = "<(/{0,1})(\\s*?)(" + sb.toString() + ")((\\s*?)|(\\s+?.*?))(/{0,1})>";
//		System.out.println(str_pattern);
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
					| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		segPosList.add(new Integer(0));
		borderList.add("<begin>");
		while (matcher.find())
		{
//			if (matcher.end() - matcher.start() > 2)
			{
				segPosList.add(new Integer(matcher.start()));
				String thisBorder = new String(originString.substring(matcher.start(), matcher.end()));
//				thisBorder = thisBorder.replaceAll("/", "");
//				thisBorder = thisBorder.replaceAll("(\\s*?)|(\\s+?.*?)", "");
				borderList.add(thisBorder);
			}
			
//			System.out.println(matcher.start() + ":" + new String(originString.substring(matcher.start(), matcher.end())));
		}
		for (int i=0; i<segPosList.size(); ++i)
		{
			if ( i < segPosList.size() - 1)
			{
				String rawText = new String(originString.substring(segPosList.get(i), segPosList.get(i+1)));
				SegmentItem thisItem = new SegmentItem(new String(eraseAllTags(rawText)), borderList.get(i), borderList.get(i+1), i);
				resultList.add(thisItem);
			}
			else
			{
				String rawText = new String(originString.substring(segPosList.get(i)));
				SegmentItem thisItem = new SegmentItem(new String(eraseAllTags(rawText)), borderList.get(i), "<end>", i);
				resultList.add(thisItem);
			}
		}
		return resultList;
	}
	public static boolean isEmptyText(String text){
		String temp = text.trim();
		temp = temp.replaceAll("\\s", "");
//		temp = temp.replaceAll("\\n", "");
		temp = temp.replaceAll("　", "");
		if (temp.length() > 0){
			return false;
		}
		return true;
	}
	public static ArrayList<String> filterEmptyText(ArrayList<String> src, ArrayList<String> dest){
		if (dest == null){
			dest = new ArrayList<String>();
		}
		if (src != null){
			for (int i=0; i<src.size(); ++i){
				if (isEmptyText(src.get(i)) == false){
					dest.add(src.get(i));
				}
			}
		}
		return dest;
	}
	public static boolean isEmptySegmentItem(SegmentItem seg){
		String temp = seg.getContent().trim();
		temp = temp.replaceAll("\\s", "");
//		temp = temp.replaceAll("\\n", "");
		temp = temp.replaceAll("　", "");
		if (temp.length() > 0){
			return false;
		}
		return true;
	}
	public static ArrayList<SegmentItem> filterEmptySegmentItem(ArrayList<SegmentItem> src, ArrayList<SegmentItem> dest){
		if (dest == null){
			dest = new ArrayList<SegmentItem>();
		}
		if (src != null){
			for (int i=0; i<src.size(); ++i){
				if (isEmptySegmentItem(src.get(i)) == false){
					dest.add(src.get(i));
				}
			}
		}
		return dest;
	}
}